/*******************************************************************************
 * Copyright (c) 2008 Sylvain Sicard.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Sylvain Sicard - initial API and implementation
 ******************************************************************************/
package fr.jade.examples.fraclite.orb.helloworld;

import fr.jade.fraclite.api.control.GenericAttributeController;
import fr.jade.fraclite.api.control.NoSuchAttributeException;

public class ServerImpl implements Service, GenericAttributeController {

  // Attributes
  protected static final String headerAttName = "header";
  protected String header = "";

  protected static final String countAttName = "count";
  protected int count = 0;

  protected static final String[] listAtt = new String[]{headerAttName, countAttName};

  // Service
  public void print (final String msg) {
    System.out.println("Server: begin printing...");
    for (int i = 0; i < count; ++i) {
      System.out.println(header + msg);
    }
    System.out.println("Server: print done.");
  }

  // GenericAttributeController
  public String getAttribute(String name) throws NoSuchAttributeException {
    if(name.equals(headerAttName)) return header;
    if(name.equals(countAttName)) return String.valueOf(count);
    throw new NoSuchAttributeException(name);
  }

  public String[] listFcAtt() {
    return listAtt;
  }

  public void setAttribute(String name, String value) throws NoSuchAttributeException {
    if(name.equals(headerAttName)) {
      header = value;
    }else if(name.equals(countAttName)) {
      count = Integer.parseInt(value);
    } else{
      throw new NoSuchAttributeException(name);
    }
  }
}
